﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class MemCountDetail
    {
        public bool Add(Chain.Model.MemCountDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemCountDetail(");
            builder.Append("CountDetailCountID,CountDetailGoodsID,CountDetailMemID,CountDetailTotalNumber,CountDetailNumber,CountDetailDiscountMoney,CountDetailPoint,CountCreateTime)");
            builder.Append(" values (");
            builder.Append("@CountDetailCountID,@CountDetailGoodsID,@CountDetailMemID,@CountDetailTotalNumber,@CountDetailNumber,@CountDetailDiscountMoney,@CountDetailPoint,@CountCreateTime)");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountDetailCountID", SqlDbType.Int, 4), new SqlParameter("@CountDetailGoodsID", SqlDbType.Int, 4), new SqlParameter("@CountDetailMemID", SqlDbType.Int, 4), new SqlParameter("@CountDetailTotalNumber", SqlDbType.Int, 4), new SqlParameter("@CountDetailNumber", SqlDbType.Int, 4), new SqlParameter("@CountDetailDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@CountDetailPoint", SqlDbType.Int, 4), new SqlParameter("@CountCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.CountDetailCountID;
            cmdParms[1].Value = model.CountDetailGoodsID;
            cmdParms[2].Value = model.CountDetailMemID;
            cmdParms[3].Value = model.CountDetailTotalNumber;
            cmdParms[4].Value = model.CountDetailNumber;
            cmdParms[5].Value = model.CountDetailDiscountMoney;
            cmdParms[6].Value = model.CountDetailPoint;
            cmdParms[7].Value = model.CountCreateTime;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Delete(int CountDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemCountDetail ");
            builder.Append(" where CountDetailID=@CountDetailID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CountDetailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemCountDetail ");
            builder.Append(" where CountDetailID in (" + CountDetailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int CountDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemCountDetail");
            builder.Append(" where CountDetailID=@CountDetailID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountDetailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CountDetailID,CountDetailCountID,CountAccount,CountDetailGoodsID,Name,CountDetailMemID,CountDetailTotalNumber,CountDetailNumber,CountDetailDiscountMoney,CountDetailPoint,MemCountDetail.CountCreateTime ");
            builder.Append(" FROM MemCountDetail,Goods,MemCount ");
            builder.Append(" where  MemCount.CountID=MemCountDetail.CountDetailCountID and Goods.GoodsID=MemCountDetail.CountDetailGoodsID ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CountDetailID,CountDetailCountID,CountDetailGoodsID,CountDetailMemID,CountDetailTotalNumber,CountDetailNumber,CountDetailDiscountMoney,CountDetailPoint,CountCreateTime ");
            builder.Append(" FROM MemCountDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.CountDetailID desc");
            }
            builder.Append(")AS Row, T.*  from MemCountDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CountDetailID", "MemCountDetail");
        }

        public DataSet GetMemCountList(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_CountList";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "CountDetailGoodsID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.MemCountDetail GetModel(int CountDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 CountDetailID,CountDetailCountID,CountDetailGoodsID,CountDetailMemID,CountDetailTotalNumber,CountDetailNumber,CountDetailDiscountMoney,CountDetailPoint,CountCreateTime from MemCountDetail ");
            builder.Append(" where CountDetailID=@CountDetailID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CountDetailID;
            Chain.Model.MemCountDetail detail = new Chain.Model.MemCountDetail();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["CountDetailID"] != null) && (set.Tables[0].Rows[0]["CountDetailID"].ToString() != ""))
            {
                detail.CountDetailID = int.Parse(set.Tables[0].Rows[0]["CountDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailCountID"] != null) && (set.Tables[0].Rows[0]["CountDetailCountID"].ToString() != ""))
            {
                detail.CountDetailCountID = int.Parse(set.Tables[0].Rows[0]["CountDetailCountID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailGoodsID"] != null) && (set.Tables[0].Rows[0]["CountDetailGoodsID"].ToString() != ""))
            {
                detail.CountDetailGoodsID = int.Parse(set.Tables[0].Rows[0]["CountDetailGoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailMemID"] != null) && (set.Tables[0].Rows[0]["CountDetailMemID"].ToString() != ""))
            {
                detail.CountDetailMemID = int.Parse(set.Tables[0].Rows[0]["CountDetailMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailTotalNumber"] != null) && (set.Tables[0].Rows[0]["CountDetailTotalNumber"].ToString() != ""))
            {
                detail.CountDetailTotalNumber = int.Parse(set.Tables[0].Rows[0]["CountDetailTotalNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailNumber"] != null) && (set.Tables[0].Rows[0]["CountDetailNumber"].ToString() != ""))
            {
                detail.CountDetailNumber = int.Parse(set.Tables[0].Rows[0]["CountDetailNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailDiscountMoney"] != null) && (set.Tables[0].Rows[0]["CountDetailDiscountMoney"].ToString() != ""))
            {
                detail.CountDetailDiscountMoney = decimal.Parse(set.Tables[0].Rows[0]["CountDetailDiscountMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountDetailPoint"] != null) && (set.Tables[0].Rows[0]["CountDetailPoint"].ToString() != ""))
            {
                detail.CountDetailPoint = int.Parse(set.Tables[0].Rows[0]["CountDetailPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CountCreateTime"] != null) && (set.Tables[0].Rows[0]["CountCreateTime"].ToString() != ""))
            {
                detail.CountCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CountCreateTime"].ToString());
            }
            return detail;
        }

        public DataSet GetQueryMemCountList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select Goods.[Name],sum(CountDetailNumber) as sumNumber FROM MemCountDetail,Goods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" and GoodsID=CountDetailGoodsID Group by Goods.[Name] ");
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemCountDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MemCountDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemCountDetail set ");
            builder.Append("CountDetailCountID=@CountDetailCountID,");
            builder.Append("CountDetailGoodsID=@CountDetailGoodsID,");
            builder.Append("CountDetailMemID=@CountDetailMemID,");
            builder.Append("CountDetailTotalNumber=@CountDetailTotalNumber,");
            builder.Append("CountDetailNumber=@CountDetailNumber,");
            builder.Append("CountDetailDiscountMoney=@CountDetailDiscountMoney,");
            builder.Append("CountDetailPoint=@CountDetailPoint,");
            builder.Append("CountCreateTime=@CountCreateTime");
            builder.Append(" where CountDetailID=@CountDetailID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CountDetailCountID", SqlDbType.Int, 4), new SqlParameter("@CountDetailGoodsID", SqlDbType.Int, 4), new SqlParameter("@CountDetailMemID", SqlDbType.Int, 4), new SqlParameter("@CountDetailTotalNumber", SqlDbType.Int, 4), new SqlParameter("@CountDetailNumber", SqlDbType.Int, 4), new SqlParameter("@CountDetailDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@CountDetailPoint", SqlDbType.Int, 4), new SqlParameter("@CountDetailID", SqlDbType.Int, 4), new SqlParameter("@CountCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.CountDetailCountID;
            cmdParms[1].Value = model.CountDetailGoodsID;
            cmdParms[2].Value = model.CountDetailMemID;
            cmdParms[3].Value = model.CountDetailTotalNumber;
            cmdParms[4].Value = model.CountDetailNumber;
            cmdParms[5].Value = model.CountDetailDiscountMoney;
            cmdParms[6].Value = model.CountDetailPoint;
            cmdParms[7].Value = model.CountCreateTime;
            cmdParms[8].Value = model.CountDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateCountDetailNumber(int intNumber, int intCountDetailID)
        {
            return DbHelperSQL.ExecuteSql(string.Format(" update MemCountDetail set CountDetailNumber = CountDetailNumber-({0}) where CountDetailID ={1}", intNumber, intCountDetailID));
        }
    }
}

